package com.community.process._1010.dao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.process._1010.dao.entity.ProcessCategory;
import com.community.process._1010.dao.mapper.ProcessCategoryMapper;
import com.community.process._1010.dao.service.ProcessCategoryIService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 到餐加工分类 服务实现类
 *
 * @author Zbs
 * @since 2024-09-05
 */
@Service
public class ProcessCategoryIServiceImpl extends ServiceImpl<ProcessCategoryMapper, ProcessCategory> implements ProcessCategoryIService {

    @Override
    public List<Long> findParentIdsByChildId(Long childId) {
        List<Long> objects = new ArrayList<>();
        getParentIds(childId, objects);
        Collections.reverse(objects);
        return objects;
    }

    public void getParentIds(Long childId, List<Long> parentIds) {
        if (childId == null) {
            return;
        }
        ProcessCategory child = this.getById(childId);
        parentIds.add(childId);
        if (child != null && child.getParentId() != null && child.getParentId() != 0) {
            this.getParentIds(child.getParentId(), parentIds);
        }
    }
}
